﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Resources;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using System.Web.UI.HtmlControls;

namespace jQuery.WebControls.ImageGallery
{
    class EstiloImageGallery : Estilo
    {
        private const string resourceZip = "iGalleryLightbox";
        private string idControl;

        public EstiloImageGallery(Page page, string idControlClient)
            : base(page)
        {
            idControl = idControlClient;
        }

        private string IdControl
        {
            get { return idControl; }
        }

        protected override string NombreCss
        {
            get { return "jquery.lightbox-0.5.css"; }
        }

        protected override string RecursoUrlCss
        {
            get { return "~/css/iGalleryLightbox/"; }
        }

        protected override void ComprobarCSS()
        {
            string pathCss = Pagina.Server.MapPath(RecursoUrlCss);

            if (!System.IO.Directory.Exists(pathCss))
            {
                string fileZIP = pathCss + resourceZip + ".zip";
                CrearCssDir(pathCss);
                ResourceManager rm = new ResourceManager(typeof(EstiloImageGallery));

                Byte[] bt = (Byte[])rm.GetObject(resourceZip);                
                FileStream fs = new FileStream(fileZIP, FileMode.Create);
                BinaryWriter bw = new BinaryWriter(fs);
                bw.Write(bt);
                bw.Close();
                fs.Close();

                FastZip zip = new FastZip();
                zip.ExtractZip(fileZIP, pathCss, "");
                zip = null;
                File.Delete(fileZIP);
            }

            if (!CheckCssHeader(Pagina.ResolveClientUrl(UrlArchivoCss)))
            {
                Pagina.Header.Controls.Add(getCssHeader(Pagina.ResolveClientUrl(UrlArchivoCss)));
            }
            AddStyleControlHeader();
        }

        private void AddStyleControlHeader()
        {
            string innerText = "#{0}{ background-color: #444; padding: 10px; width: 520px;}" +
                "#{0} ul { list-style: none; }" +
                "#{0} ul li { display: inline; }" +
                "#{0} ul img { border: 5px solid #3e3e3e;border-width: 5px 5px 20px;}" +
                "#{0} ul a:hover img { border: 5px solid #fff; border-width: 5px 5px 20px;color: #fff;}" +
                "#{0} ul a:hover { color: #fff; }";

            innerText = innerText.Replace("{0}", IdControl);
            HtmlGenericControl style = new HtmlGenericControl("style");
            style.Attributes.Add("type", "text/css");
            style.InnerText = innerText;
            Pagina.Header.Controls.Add(style);
        }
    }
}
